

******************************************************************************************
* Create elasticity of time to sale and price by city
******************************************************************************************

local file_path_in "/tochange/"

set more off

use "`file_path_in'//county transactions/elasticity_cleaned.dta", clear

sort fips_code listdate

** filters
bysort fips_code : egen firstdate = min(listdate)
format firstdate %td
drop if firstdate>td(01jan2002)  /* drop counties that do not appear in MLS until after 2002 */

keep if listdate>=td(01jan2002) & listdate<td(01jan2009)  

drop if time_mkt<0 

gen one = 1 if !mi(ClosePrice) & !mi(close_month)  & Baths!="" & PropertyType!=""  /* drop counties that do not have many sales */
bysort fips_code: gen sumc = sum(one)
bysort fips_code: replace sumc = sumc[_N]
drop if sumc<1000

** replicate Guren's aggregate relationship 
set matsize 10000

* 0. clean data
encode PropertyType, gen(property_type)
destring fips_code, replace
destring BathsTotal, gen(baths)
gen close_year = yofd(dofm(close_month))
g d = time_mkt<91
g lncloseprice = log(ClosePrice)

* 1. predicted price: p_hat = eta + q
keep if close_year<2010 
reg  lncloseprice baths  i.property_type i.fips_code i.close_year if close_year>=2007
predict lnp_hat, xb
g p_gap = lncloseprice - lnp_hat

binscatter lnp_hat lncloseprice 
graph export "`file_path_in'/reg output//binscatter_pred_price.png", replace							

local file_path_in "/tochange/"

* Figure 1 of Guren:
binscatter d p_gap, xtitle("Log Relative Markup") ytitle("Prob. of Sale in 13 Weeks") linetype(none)
graph export "`file_path_in'/reg output//binscatter_pred_price.png", replace

* 2. get elasticity of time to sale and log markup
egen gap_bucket = cut(p_gap), at(-1.2(0.2)1.2) label						
reghdfe d ib7.gap_bucket , absorb(i.fips_code i.close_year)
eststo c1
local file_path_in "/tochange/"
coefplot c1, keep(*.gap_bucket) vertical  byopts(yrescale)  xtitle("Log Relative Markup")  ytitle("Probability of Sale within 91 Days") graphregion(color(white))
graph export "`file_path_in'/reg output//linear_pred_elasticity.png", replace

save "`file_path_in'//county transactions/elasticity_final.dta", replace

use "`file_path_in'//county transactions/elasticity_final.dta", clear

***********************************************************************************************
* for each fips, will run this regression: reghdfe d p_gap, absorb(i.fips_code##i.close_month)
***********************************************************************************************

set matsize 10000

cap drop  lnp_hat p_gap gap_bucket
drop if fips==08035
levelsof fips_code, local(fips)

cap matrix drop A 
local rownms 

foreach w of local fips {
	di "`w'"
	reg  lncloseprice baths  i.property_type i.close_month  if fips_code==`w'
	di "`w'"
	predict lnp_hat, xb
	g p_gap = lncloseprice - lnp_hat
	summ d if fips_code==`w'
	local avg_d `r(mean)'
	reghdfe d p_gap if fips_code==`w' & p_gap>=0 & p_gap<=1, absorb(i.close_month) 
	di "`w'"
	local slope `=round(_b[p_gap],0.0001)'
	local elasticity `=round(_b[p_gap]*`avg_d',0.0001)'
	mat A = nullmat(A) \ (`w', _b[p_gap], _b[p_gap]/_se[p_gap],  e(r2), e(N), `avg_d')  
	local rownms `rownms' fips:`w'
	
	cap drop gap_bucket 
	drop lnp_hat p_gap 
}

mat coln A = f b t r2 n d 
mat rown A = `rownms'

matrix list A
	
svmat double A, name(bvector)
list bvector1-bvector6 in 1/6
keep bvector1-bvector6

rename bvector1 fipscode
rename bvector2 elasticity_b
rename bvector3 elasticity_se
rename bvector4 elasticity_r2
rename bvector5 elasticity_N
rename bvector6 elasticity_d

tostring fipscode, replace
replace fipscode = "0"+fipscode if strlen(fipscode)==4
destring fipscode, gen(fips)

keep if fips!=.

save "`file_path_in'//county transactions/elasticity_tomerge.dta", replace
